import 'package:enterprise_v6_app/app/components/custom_refresh.dart';
import 'package:enterprise_v6_app/app/components/dialog/filter_dialog/custom_filter_bottom_sheet.dart';
import 'package:enterprise_v6_app/app/components/toast_widget.dart';
import 'package:enterprise_v6_app/app/core/constans/hazard_investigation_code.dart';
import 'package:enterprise_v6_app/app/core/core.dart';
import 'package:enterprise_v6_app/app/core/ext/widget_extension.dart';
import 'package:enterprise_v6_app/app/core/http/http.dart';
import 'package:enterprise_v6_app/app/core/utils/interface.dart';
import 'package:enterprise_v6_app/app/data/entities/application/filter/filter_bean.dart';
import 'package:enterprise_v6_app/app/data/entities/application/filter/filter_entities_bean.dart';
import 'package:enterprise_v6_app/app/data/entities/application/special_list/process_nodes_entity.dart';
import 'package:enterprise_v6_app/app/data/entities/task/base_danger_investigation_entity.dart';
import 'package:enterprise_v6_app/router/app_pages.dart';

class HazardManagementController extends GetxController {
  /// 搜索使用key
  final GlobalKey<CustomEasyRefreshListState> globalKey = GlobalKey();

  /// 筛选右侧栏控制Key
  final GlobalKey<ScaffoldState> popRightScaffoldKey = GlobalKey();

  /// 请求参数
  Map<String, dynamic> queryParameters = {};

  /// 搜索字符串
  String? search;

  /// 当前执行节点
  ProcessNodesEntity implementNode = ProcessNodesEntity();

  /// 筛选列表
  List<FilterBean> filterList = [];

  /// 隐患类型
  int? type;

  @override
  void onInit() {
    super.onInit();
    initFilter();
  }

  /// 初始化筛选值
  initFilter() async {
    //添加作业状态筛选
    final List<FilterEntitiesBean> hazardType = [];
    hazardType.add(FilterEntitiesBean(name: '待确认', value: -1));
    hazardType.add(FilterEntitiesBean(name: '待整改', value: 0));
    hazardType.add(FilterEntitiesBean(name: '待验收', value: 1));
    final FilterBean workTypeFilterBean = FilterBean(title: '隐患状态', filterList: hazardType);
    filterList.add(workTypeFilterBean);
  }

  /// 获取数据
  Future<List<BaseDangerInvestigationEntity>> getListData(int pageNum, int pageSize) async {
    queryParameters = {};
    queryParameters['pageNum'] = pageNum;
    queryParameters['pageSize'] = pageSize;
    if (search != null && search!.isNotEmpty) {
      queryParameters['dangerRecordDesc'] = search;
    }
    if (type != null) {
      queryParameters['type'] = type;
    }
    // 获取作业列表接口
    final res =
        await HttpUtil().generateGetRequest<List<BaseDangerInvestigationEntity>>(Interface.getListNoConfirm, queryParameters: queryParameters).execute();
    List<BaseDangerInvestigationEntity>? data;
    res.onSuccess((result) {
      data = result.data;
    });
    return data ?? [];
  }

  /// 点击筛选
  void screen() {
    // popRightScaffoldKey.currentState?.openEndDrawer();
    CustomFilterBottomSheet(
      dataList: filterList,
      onConfirm: (List<FilterBean> value) {
        // 选中回调
        filterList = value;
        for (var i in value) {
          if (i.title == '隐患状态') {
            final chosenValue = i.chosenValue;
            type = chosenValue[0].value as int;
          }
        }
        refreshPage();
      },
    ).showBottomSheetDialog();
  }

  /// 点击任务
  /// 根据任务状态进入不同页面
  Future<void> clickOnTask(BaseDangerInvestigationEntity entity) async {
    //获取数据
    final res = await HttpUtil().generateGetRequest<List<ProcessNodesEntity>>(Interface.getProcessNodesByBusinessId,
        queryParameters: {'processEncoding': HazardInvestigationCode.processEncoding, 'businessId': entity.id}).execute();
    //查询到流程节点数据
    res.onSuccess((result) async {
      if (result.data!.isNotEmpty) {
        final List<ProcessNodesEntity> nodeList = result.data!;
        for (var node in nodeList) {
          //找到当前执行节点
          if (node.executableOrNot == 1) {
            // 点击项的执行节点
            implementNode = node;
            switch (node.appRoutingAddress) {
              // 隐患确认-进入隐患确认页面
              case HazardInvestigationCode.hazardConfirmation:
                Get.toNamed(AppRoutes.hazardConfirmationPage, arguments: entity.id);
                break;
              // 隐患整改-进入隐患整改页面
              case HazardInvestigationCode.hazardRectification:
                Get.toNamed(AppRoutes.hazardRectificationPage, arguments: {
                  'id': entity.id,
                  'isAcceptance': false,
                });
                break;
              // 隐患验收-进入隐患验收页面
              case HazardInvestigationCode.hazardAcceptance:
                Get.toNamed(AppRoutes.hazardRectificationPage, arguments: {
                  'id': entity.id,
                  'isAcceptance': true,
                });
                break;
              default:
                AppToast.showWarning('数据异常,未查询到流程数据!');
                break;
            }
            break;
          }
        }
      } else {
        AppToast.showWarning('数据异常,未查询到流程数据!');
      }
    });
    // 待确认-进入隐患确认页面
    // Get.toNamed(AppRoutes.hazardConfirmationPage, arguments: '1');

    // 待整改-进入隐患整改页面
    // Get.toNamed(AppRoutes.hazardRectificationPage, arguments: {
    //   'taskId': '1',
    //   'isAcceptance': false,
    // });
    // 待验收-进入隐患验收页面
    // Get.toNamed(AppRoutes.hazardRectificationPage, arguments: {
    //   'taskId': '1',
    //   'isAcceptance': true,
    // });
  }

  /// 搜索
  void refreshPage() {
    globalKey.currentState?.onRefresh();
  }
}
